import BaseService from './BaseService.js';

class CommonService extends BaseService {
  constructor() {
    super()
    // `$$prefix` 将自动加在 `url` 前面，除非 `url` 是一个绝对 URL
    this.$$path = {
      login: '/oauth/token',
      listProductTypes: '/v0/common/product-type/list',
      insertCitys: '/users/insertCitys',
      queryCityGroup: '/users/queryCityList'
    }
  }
  login() {
    const _ = this;
    return new Promise(function (resolve, reject) {
      wx.login({
        success: res => {
          _.get(`${_.$$path.login}/${res.code}`,null, false).then(result=>{
            if (result.status == 0) {
              const { 
                token, 
                userInfo, 
                config, 
                productTypes, 
                cityGroup,
                settingConfig
              } = result.data;
              wx.setStorageSync('token', token);
              wx.setStorageSync('user', userInfo);
              wx.setStorageSync('config', config);
              wx.setStorageSync('productTypes', productTypes);
              wx.setStorageSync('citys', cityGroup);
              wx.setStorageSync('setting', settingConfig);
              resolve(token);
            }else{
              reject('获取token失败');
            }
          });
        }
      });
    });
  }
  listProductTypes(){
    return this.get(this.$$path.listProductTypes)
  }
  appInit(){
    const listProductTypes = this.listProductTypes();
    listProductTypes.then(result=>{
      if(result.status == 0){
        wx.setStorageSync('productTypes', result.data);
      }
    })
    const queryCityGroup = this.queryCityGroup();
    queryCityGroup.then(result=>{
      if (result.status == 0) {
        wx.setStorageSync('citys', result.data);
      }
    })
    return Promise.all([listProductTypes, queryCityGroup]);
  }
  insertCitys(param){
    return this.post(this.$$path.insertCitys, {data:param});
  }
  queryCityGroup() {
    return this.get("/common/citys/group");
  }
  /**获取统一下单数据 */
  payUnifiedorder(orderCode){
    return this.get(`wx/unifiedorder/${orderCode}`);
  }
  /**首页统计数据 */
  homeCount(){
    return this.get("/v0/common/home/count");
  }
  /**获取广告图 */
  listAd(param){
    return this.post("/v0/ad/list", { data: param});
  }
  /**修改活跃分数 */
  active(type) {
    return this.post(`v0/user/active/${type}`)
  }
  getValidCode(phone){
    return this.get(`v0/common/valid/code/${phone}`)
  }
}

export default CommonService;
